//
//  GLBadgeViewController.m
//  GLFormKitDemo
//
//  Created by 121 on 2018/3/28.
//  Copyright © 2018年 杭州吉利控股信息中心. All rights reserved.
//

#import "GLBadgeViewController.h"
#import "GLBadgeLabel.h"
#import <Masonry/Masonry.h>

@interface GLBadgeViewController ()

@property(nonatomic,strong)NSArray* datas;

@end

@implementation GLBadgeViewController

-(void)viewDidLoad{
    
    [super viewDidLoad];
    self.datas = @[@{@"keyPath":@"com.app.news"},
                   @{@"keyPath":@"com.app.mall"},
                   @{@"keyPath":@"com.app"},
                   @{@"keyPath":@"com.app"},
                   @{@"keyPath":@"com"},
                   @{@"keyPath":@"com.app.news.tiyu",@"value":@3},
                   @{@"keyPath":@"com.app.news.keji",@"value":@1},
                   @{@"keyPath":@"com.app.news.yule",@"value":@2},
                   @{@"keyPath":@"com.app.mall.number",@"value":@3},
                   @{@"keyPath":@"com.app.mall.update",@"value":@1},
                   @{@"keyPath":@"com.car.conn.number",@"value":@3},
                   ];
    
    self.form = [self form0];
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn showBadgeBindKeyPath:@"com.app" center:CGPointZero badgeColor:[UIColor blueColor]];
    
    
    UIBarButtonItem* item = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = item;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        for (NSDictionary* dict in self.datas) {
            NSString* title = dict[@"keyPath"];
            NSNumber* value = dict[@"value"];
            if (value) {
                [[GLBadgeCenter defaultCenter]setValue:[value integerValue] forKeyPath:title];
            }
        }
    });
    
    
}
-(void)dealloc{
    NSLog(@"");
}


- (void)formRowClicked:(GLFormRow*)row{
    [super formRowClicked:row];
    
}

-(void)formRow:(GLFormRow*)row eventClicked:(id)sender subTag:(NSString*)subTag{
    
    
    NSIndexPath* indexPath = [self indexPathForFormRow:row];
    NSDictionary* dict = [self.datas objectAtIndex:indexPath.row];
    NSString* keyPath = dict[@"keyPath"];
    NSInteger value = [[GLBadgeCenter defaultCenter]valueForKeyPath:keyPath];
    
    if ([subTag isEqualToString:GLCustomCellButton]) {
        value++;
    }
    else{
        value--;
    }
    if (value < 0) {
        return;
    }
    [[GLBadgeCenter defaultCenter]setValue:value forKeyPath:keyPath];
}
-(void)formRow:(GLFormRow*)row valueChanged:(id)newValue{
    NSLog(@"valueChanged--->%@",newValue);
}


-(GLForm*)form0{
    GLForm* form = [GLForm formWithTag:@"form"];
    
    GLFormSection* section = [GLFormSection formSectionWithTag:@""];
    
    
    for (NSDictionary* dict in self.datas) {
        NSString* title = dict[@"keyPath"];
        
        NSRegularExpression* ex = [NSRegularExpression regularExpressionWithPattern:@"\\." options:NSRegularExpressionCaseInsensitive error:nil];
        NSArray* result = [ex matchesInString:title options:NSMatchingReportCompletion range:NSMakeRange(0, title.length)];
        if (result.count >= 3) {
            GLFormRow* row1 = [GLFormRow formRowByComponentKeys:@[GLCustomCellText,GLCustomCellButton,GLCustomCellButton2,GLCustomCellCatpionText,GLCustomCellBadge] withTag:@"cell"];
            [row1 addConfigValue:title componentKey:GLCustomCellText forKeyPath:nil];
            [row1 addConfigValue:title componentKey:GLCustomCellBadge forKeyPath:@"bindKeyPath"];
            [row1 addConfigValue:@"加" forKeyPath:GLCustomCellButton];
            [row1 addConfigValue:@"减" forKeyPath:GLCustomCellButton2];
            [section addFormRow:row1];
        }else{
            GLFormRow* row1 = [GLFormRow formRowByComponentKeys:@[GLCustomCellText,GLCustomCellBadge] withTag:@"cell"];
            [row1 addConfigValue:title componentKey:GLCustomCellText forKeyPath:nil];
            [row1 addConfigValue:title componentKey:GLCustomCellBadge forKeyPath:@"bindKeyPath"];
            
            
            [section addFormRow:row1];
        }
        
        
    }
    
    [form addFormSection:section];
    
    return form;
    
}




@end
